package com.jae.model;

import com.jae.model.user.User;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.Collections;

/**
 * 重写UserDetails
 * 因为鉴权通过后想知道是哪个用户在操作，不能每次都通过username去找数据库
 * 所以索性将登录后的用户放在UserDetails里，到时可以直接通过getUser()来获取
 */
@Data
@NoArgsConstructor
public class SecurityUser implements UserDetails {
    private String username;
    private String password;
    protected User user;
    public SecurityUser(User user) {
        this.user = user;
    }

    public User getUser() {
        return user;
    }
    //    权限角色集合
    private Collection<? extends GrantedAuthority> authorities;

    public SecurityUser(String username,String password,Collection<? extends GrantedAuthority> authorities){
        this.username = username;
        this.password = password;
        this.authorities = authorities;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if (user == null) {
            return Collections.emptySet();
        }
        return user.getAuthorities();
    }

    @Override
    public String getPassword() {
        return user.getPassword();
    }

    @Override
    public String getUsername() {
        return user.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }


}
